package com.forum.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.forum.mapper.ForumCommentMapper;
import com.forum.mapper.StudentInfoMapper;
import com.forum.pojo.*;
import com.forum.pojo.vo.CommentTipOffVo;
import com.forum.service.CommentTipoffInfoService;
import com.forum.mapper.CommentTipoffInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author 86152
* @description 针对表【comment_tipoff_info】的数据库操作Service实现
* @createDate 2023-02-21 16:21:21
*/
@Service
public class CommentTipoffInfoServiceImpl extends ServiceImpl<CommentTipoffInfoMapper, CommentTipoffInfo>
    implements CommentTipoffInfoService{
    @Autowired
    CommentTipoffInfoMapper commentTipoffInfoMapper;
    @Autowired
    StudentInfoMapper studentInfoMapper;
    @Autowired
    ForumCommentMapper forumCommentMapper;
    @Override
    public void saveTipOff(Long userId, Integer commentId, String description) {
        QueryWrapper<ForumComment> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("comment_id",commentId);

        Long informedUserId = forumCommentMapper.selectOne(queryWrapper).getUserId();
        CommentTipoffInfo commentTipoffInfo=new CommentTipoffInfo();
        commentTipoffInfo.setCommentId(commentId);
        commentTipoffInfo.setUserId(userId);
        commentTipoffInfo.setInformedUserId(informedUserId);
        commentTipoffInfo.setDescription(description);
        commentTipoffInfoMapper.insert(commentTipoffInfo);

        StudentInfo studentInfo = studentInfoMapper.selectById(informedUserId);
        studentInfo.setIllegalcount(studentInfo.getIllegalcount()+1);
        studentInfoMapper.updateById(studentInfo);

    }

    @Override
    public Page<CommentTipoffInfo> getTipOff(Integer pageNo, Integer pageSize) {
        List<CommentTipoffInfo> commentTipoffInfos = commentTipoffInfoMapper.selectList(null);
        List<CommentTipOffVo> result=new ArrayList<>();
        for (CommentTipoffInfo commentTipoff:commentTipoffInfos) {
            CommentTipOffVo commentTipOffVo=new CommentTipOffVo();
            QueryWrapper<ForumComment> queryWrapper=new QueryWrapper<>();
            QueryWrapper<StudentInfo> wrapper1=new QueryWrapper<>();

            //获得举报人名字
            wrapper1.eq("studentId",commentTipoff.getUserId());
            
            queryWrapper.eq("comment_id", commentTipoff.getCommentId());
            ForumComment forumComment = forumCommentMapper.selectOne(queryWrapper);
            if(forumComment!=null){
                commentTipOffVo.setGoodCount(forumComment.getGoodCount());
                commentTipOffVo.setContent(forumComment.getContent());
                commentTipOffVo.setNickName(forumComment.getNickName());
                commentTipOffVo.setUserId(forumComment.getUserId());
            }
            commentTipOffVo.setTioffId(commentTipoff.getTipoffId());
            commentTipOffVo.setDesc(commentTipoff.getDescription());
            commentTipOffVo.setCommentId(commentTipoff.getCommentId());
            commentTipOffVo.setUserNickName(studentInfoMapper.selectOne(wrapper1).getNickname());
            result.add(commentTipOffVo);
        }
        return getPages(pageNo,pageSize,result);
    }


    public static Page getPages(Integer currentPage, Integer pageSize, List list){

        Page page = new Page();

        int size = list.size();
        if(size==0){
            return null;
        }
        if(pageSize > size){
            pageSize = size;
        }

        //求出最大页数，防止currentPage越界
        int maxPage = size % pageSize == 0 ? (size / pageSize) : (size / pageSize) + 1;

        if(currentPage > maxPage){
            currentPage = maxPage;
        }

        //当前页第一条数据下标
        int curIds = currentPage > 1 ? (currentPage -1) * pageSize : 0;

        List pageList = new ArrayList<>();

        //将当前页的数据放进pageList
        for (int i = 0; i < pageSize && curIds + i < size; i++) {
            pageList.add(list.get(curIds + i));
        }

        page.setCurrent(currentPage).setSize(pageSize).setTotal(list.size()).setRecords(pageList);

        return page;

    }
}




